import {handleActions} from 'redux-actions';
import {get_data, loading, finished} from "../types/home";

import stringUtil from '../../common/utils/stringUtil';
import httpUtil from '../../common/utils/httpUtil';

export default handleActions({

  [get_data](state, action) {

    return httpUtil.myReducers(state, action, function (sucState, sucData) {

      if (stringUtil.isEmptyObject(sucData)) return;

      let totalPage = sucData.totalPage;
      let newGoodsList = sucData.list;

      if (stringUtil.isEmptyObject(newGoodsList)) return;
      let oldList = sucState.goodsList;

      let tempResult = {};

      tempResult.goodsList = oldList.concat(newGoodsList);
      tempResult.currentPage = sucState.currentPage + 1;
      tempResult.totalPage = totalPage;

      // if (tempResult.currentPage > tempResult.totalPage) {
      //   tempResult.finished = true;
      // }
      tempResult.loading = false;

      return tempResult;
    }, function (errState, errData) {

      console.log(errData.msg);

      return {
        test: errState.test + '     error啦'
      };

    });
  },

  [loading](state, action) {

    let temp = action.payload;
    return {
      ...state,
      loading: temp
    };
  },

  [finished](state, action){
    let finish = action.payload;
    return {
      ...state,
      finished: finish
    };
  }

}, {
  // 商品列表
  goodsList: [],

  currentPage: 1,

  totalPage: 1,

  test: 'hello！！！',

  loading: false,

  finished: false


});
